﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.Common;
using StoragePeriod.Data;
using StoragePeriod.Web.Common;
using System.Text;
using StoragePeriod.Model.StorageForecast;
using StoragePeriod.BLL.StorageForecast;
using StoragePeriod.BLL.RawDict;
using StoragePeriod.Model.RawDict;

namespace StoragePeriod.Web.StorageForecast.ChangeExamples
{
    public partial class ChangeExamplesList : SecurityPage
    {
        T_RawMaterial tRawMaterial = new T_RawMaterial();   //原料操作
        T_QualityCalculateMove tQualityCalculateMove = new T_QualityCalculateMove(); //品质估算模型
        T_QualityCalculateLog tQualityCalculateLog = new T_QualityCalculateLog();//品质估算日志
        T_QualityResult tQualityResult = new T_QualityResult();//品质估算结果
        T_ImgView tImgView = new T_ImgView();//品质估算图片预览

        public override string PrivCode()
        {
            return "fd31e9e19a15dc48";
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            BindTreeView();
            LeftLable.Text = "品质变化实例";
            this.LoadPriv();
        }

        //加载权限控件
        private void LoadPriv()
        {
            this.Btn_AddImg.Visible = IsExisPriv("dd10b3c59866e745");
            this.Btn_DelImg.Visible = IsExisPriv("722fae95320532ab");
        }

        //绑定原料树
        public void BindTreeView()
        {
            NutrientsTree.Nodes.Clear();
            List<RawMaterialType> RawTypeList = RawMaterialType.Where("ParentId=@0 and Typeinfoid=@1", 0, Convert.ToInt32(EnumData.TypeInfo.食品检测)).ToList();
            RawTypeList.ForEach(p =>
            {
                TreeNode childNode = new TreeNode()
                {
                    Text = p.TypeName,
                    Value = p.id.EToString(),
                    Target = "Parent",
                };
                childNode = GetRawByTypeId(childNode);
                NutrientsTree.Nodes.Add(childNode);
            });
            SysTree_SelectedNodeChanged(null, null);
        }
        private TreeNode GetRawByTypeId(TreeNode tn)
        {
            int Pid = int.Parse(tn.Value);
            List<RawMaterialType> RawTypeList = RawMaterialType.Where("ParentId=@0", Pid).ToList();
            RawTypeList.ForEach(p =>
            {
                TreeNode childNode = new TreeNode()
                {
                    Text = p.TypeName,
                    Value = p.id.ToString(),
                    Target = "Parent"
                };
                childNode = GetRawByTypeId(childNode);
                tn.ChildNodes.Add(childNode);
            });

            int TypeId = int.Parse(tn.Value);
            List<E_RawMaterial> rmlist = rmlist = tRawMaterial.GetModelList("PeriodTypeStorageID =" + TypeId);

            rmlist.ForEach(a =>
            {
                TreeNode rnod = new TreeNode()
                {
                    Text = a.RAWName,
                    Value = a.id.EToString(),
                    Target = "Child"
                };
                tn.ChildNodes.Add(rnod);
            });
            return tn;
        }

        protected void SysTree_SelectedNodeChanged(object sender, EventArgs e)
        {
            TreeNode tn = this.NutrientsTree.SelectedNode;
            if (tn != null && tn.Target == "Child")
            {
                this.HidRawID.Value = tn.Value;
                this.RepImgListBind();//绑定品质图片
            }
        }

        //删除 贮存知识
        protected void btn_Del_Click(object sender, EventArgs e)
        {
        }

        //查询原料树
        protected void btn_search_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Txt_Search.Text))
            {
                return;
            }
            for (int i = 0; i < NutrientsTree.Nodes.Count; i++)
            {
                for (int ii = 0; ii < NutrientsTree.Nodes[i].ChildNodes.Count; ii++)
                {
                    if (NutrientsTree.Nodes[i].ChildNodes[ii].Text.Contains(Txt_Search.Text))
                    {
                        TreeNode node = NutrientsTree.Nodes[i].ChildNodes[ii];
                        string text = node.Text;
                        node.Text = HttpUtility.HtmlDecode(node.Text).Replace(Txt_Search.Text, "<span class='Red'>" + Txt_Search.Text + "</span>");
                    }
                }
            }
        }

        //保存图片
        protected void btn_SaveImg_Click(object sender, EventArgs e)
        {
            string strFileName = this.file_ImgPath.FileName;
            string[] strS = strFileName.Split('.');
            string strName = Guid.NewGuid() + "." + strS[strS.Length - 1];
            this.file_ImgPath.SaveAs(MapPath("~/UpLoad/") + strName);

            E_ImgView eImgView = new E_ImgView();
            eImgView.RawID = Convert.ToInt32(this.HidRawID.Value);
            eImgView.ImgTypeID = 2;//品质变化实例
            eImgView.Imgname = this.txt_addImgName.Text.Trim();
            eImgView.ImgPath = strName;
            eImgView.UpLoadTime = DateTime.Now;
            eImgView.UpLoadUserID = CurrentUser.id;
            eImgView.UpLoadUserName = CurrentUser.UserName;
            eImgView.Description = this.txt_addImgDescription.Text.Trim();
            tImgView.Add(eImgView);

            this.HidPageIndex.Value = "1";
            this.RepImgListBind();
        }

        //删除图片
        protected void Btn_DelImg_Click(object sender, EventArgs e)
        {
            int ImgID = Convert.ToInt32(this.HidImgID.Value);
            if (ImgID != 0)
            {
                tImgView.Delete(ImgID);
            }
            this.HidPageIndex.Value = "1";
            this.HidIsNext.Value = "1";
            this.RepImgListBind();
        }

        //绑定图片列表
        private void RepImgListBind()
        {
            int RawID = Convert.ToInt32(this.HidRawID.Value);
            int pageindex = Convert.ToInt32(this.HidPageIndex.Value);
            int pagesize = 5;

            //获取品质预览图片集合
            List<E_ImgView> list = tImgView.GetModelList("RawID=" + RawID + " and ImgTypeID=2");
            int rowcount = list.Count();
            this.HidPageCount.Value = ((rowcount % pagesize > 0) ? rowcount / pagesize + 1 : rowcount / pagesize).ToString();



            this.RepImgList.DataSource = list.OrderByDescending(p => p.UpLoadTime).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList();
            this.RepImgList.DataBind();

            //加载默认指定JS
            UtilityUI.Load(this.UpdatePanel1, "SetShowPage();");
        }

        //上一页
        protected void Btn_List_Prve_Click(object sender, EventArgs e)
        {
            int pageindex = Convert.ToInt32(this.HidPageIndex.Value);
            int pagecount = Convert.ToInt32(this.HidPageCount.Value);

            if (pageindex > 1)
            {
                this.HidPageIndex.Value = (pageindex - 1).ToString();
                this.RepImgListBind();
            }
        }

        //下一页
        protected void Btn_List_Next_Click(object sender, EventArgs e)
        {
            int pageindex = Convert.ToInt32(this.HidPageIndex.Value);
            int pagecount = Convert.ToInt32(this.HidPageCount.Value);

            if (pageindex < pagecount)
            {
                this.HidPageIndex.Value = (pageindex + 1).ToString();
                this.RepImgListBind();
            }
        }
    }
}